import 'dart:io';

import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class DialogUtil {
  static Future<int> showAlertDialog(BuildContext context,
      {String title,
      Widget titleWidget,
      String content,
      Widget contentWidget,
      String cancel,
      Widget cancelWidget,
      String ensure,
      Widget ensureWidget,
      bool barrierDismissible = true}) async {
    Widget widgetTitle = titleWidget != null
        ? titleWidget
        : title != null
            ? Text(title)
            : null;
    Widget widgetContent = contentWidget != null
        ? contentWidget
        : content != null
            ? Text(
                content,
                textAlign: TextAlign.left,
              )
            : null;
    Widget widgetCancel = cancelWidget != null
        ? cancelWidget
        : !TextUtil.isEmpty(cancel)
            ? Platform.isIOS
                ? CupertinoButton(
                    child: Text(
                      cancel,
                      style: Theme.of(context)
                          .textTheme
                          .caption
                          .copyWith(fontSize: 14),
                    ),
                    onPressed: () => Navigator.of(context).pop(0),
                  )
                : FlatButton(
                    child: Text(
                      cancel,
                      style: Theme.of(context)
                          .textTheme
                          .caption
                          .copyWith(fontSize: 14),
                    ),
                    onPressed: () => Navigator.of(context).pop(0),
                  )
            : null;
    Widget widgetEnsure = ensureWidget != null
        ? ensureWidget
        : !TextUtil.isEmpty(ensure)
            ? Platform.isIOS
                ? CupertinoButton(
                    child: Text(
                      ensure,
                      style: Theme.of(context).textTheme.caption.copyWith(
                            fontSize: 14,
                            color: Theme.of(context).accentColor,
                          ),
                    ),
                    onPressed: () => Navigator.of(context).pop(1),
                  )
                : FlatButton(
                    child: Text(
                      ensure,
                      style: Theme.of(context).textTheme.caption.copyWith(
                            fontSize: 14,
                            color: Theme.of(context).accentColor,
                          ),
                    ),
                    onPressed: () => Navigator.of(context).pop(1),
                  )
            : null;
    return await showDialog<int>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) => Platform.isIOS
            ? CupertinoAlertDialog(
                title: widgetTitle,
                content: widgetContent,
                actions: [widgetCancel, widgetEnsure],
              )
            : AlertDialog(
                title: widgetTitle,
                content: widgetContent,
                elevation: 0,
                actions: [widgetCancel, widgetEnsure],
              ));
  }
}
